package com.qbccn.onlinetest.controller;

import com.qbccn.onlinetest.config.KeyConfig;
import com.qbccn.onlinetest.config.custom.DataException;
import com.qbccn.onlinetest.dto.ResponseResult;
import com.qbccn.onlinetest.service.CompanyService;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author linke
 * @date 2020/7/4 2:07
 * @description
 */
@Controller
public class CommonController {

  @Autowired
  private HttpServletRequest request;
  @Autowired
  private CompanyService companyService;

  @GetMapping("clear")
  @ResponseBody
  @CacheEvict(cacheNames = {KeyConfig.styleKey}, allEntries = true)
  public String clear() {
    return "清除缓存成功！";
  }

  @GetMapping("")
  public String index() {
    return "index";
  }

  @GetMapping("login")
  public String login() {
    return "login";
  }

  @GetMapping("register")
  public String register() {
    return "register";
  }

  @GetMapping("getCompanySelect")
  public String getCompanySelect() {
    return "common/companySelectPage";
  }

  @GetMapping("getCompanyData")
  @ResponseBody
  public ResponseResult getCompanyData(@RequestParam("companyCode") String companyCode,
      @RequestParam("level") Integer level, @RequestParam("findSub") Boolean findSub) {
    String username = request.getSession().getAttribute("username").toString();
    if (StringUtils.isEmpty(level)) {
      throw new DataException("level 不能为空");
    } else if (StringUtils.isEmpty(companyCode)) {
      throw new DataException("companyCode 不能为空");
    }
    return new ResponseResult(0,
        companyService.getCompanyData(username, companyCode, level.intValue(), findSub));
  }
}
